package com.hfb.mashibing.alip8.system.io;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

/** Socket Server
 * BIO 多线程的方式
 */
public class T02_BIO_SocketIOProperties {

    // server socket listen property
    private static final int RECEIVE_BUFFER = 10;
    private static final int SO_TIMEOUT = 0;
    private static final boolean REUSE_ADDR = false;
    private static final int BACK_LOG = 2; // 排队等待最大长度，默认50

    // client socket listen property on server endpoint
    private static final boolean CLI_KEEPALIVE = false;
    private static final boolean CLI_OOB = false;
    private static final boolean CLI_REC_ADDR = false;
    private static final int CLI_SEND_BUF = 20;
    private static final boolean CLI_LINGER = true;
    private static final int CLI_LINGER_N = 0;
    private static final int CLI_TIMEOUT = 0;
    private static final boolean CLI_NO_DELAY = false;

    private static final int SERVERSOCKET_PORT = 9090;

    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            server = new ServerSocket();
            server.bind(new InetSocketAddress(SERVERSOCKET_PORT), BACK_LOG);
            server.setReceiveBufferSize(RECEIVE_BUFFER);
            server.setReuseAddress(REUSE_ADDR);
            server.setSoTimeout(SO_TIMEOUT);

            System.out.println(String.format("server up use %s!", SERVERSOCKET_PORT));

            while (true) {
                try {
                    System.in.read();   // 分水岭

                    Socket client = server.accept();
                    System.out.println("client port:" + client.getPort());

                    client.setKeepAlive(CLI_KEEPALIVE);
                    client.setOOBInline(CLI_OOB);
                    client.setReceiveBufferSize(RECEIVE_BUFFER);
                    client.setReuseAddress(REUSE_ADDR);
                    client.setSendBufferSize(CLI_SEND_BUF);
                    client.setSoLinger(CLI_LINGER, CLI_LINGER_N);
                    client.setSoTimeout(SO_TIMEOUT);
                    client.setTcpNoDelay(CLI_NO_DELAY);

                    new Thread(()-> {
                        while (true) {
                            try (InputStream in = client.getInputStream()) {
                                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                                char[] data = new char[1024];
                                int num = reader.read(data);
                                if (num > 0) {
                                    System.out.println(
                                        "client read some data is :" + num + " val:" + new String(data,
                                            0, num));
                                } else if (num == 0) {
                                    System.out.println("client readed nothing!");
                                    continue;
                                } else {
                                    System.out.println("client read -1");
                                    client.close();
                                    break;
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(server)) {
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
